package de.lulusoft.anothertorrent.gui.views.Providers;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;

import de.lulusoft.anothertorrent.core.rtorrent.Torrent;
import de.lulusoft.anothertorrent.gui.commands.ChangeTorrentPriorityCommand;

public class TorrentListViewPriorityEditingSupport extends EditingSupport {
	
	private CellEditor editor;

	public TorrentListViewPriorityEditingSupport(ColumnViewer viewer) {
		super(viewer);
		
		editor = new ComboBoxCellEditor(((TableViewer) viewer).getTable(), 
				new String[] {"off", "low", "normal", "high"});

	}

	@Override
	protected boolean canEdit(Object element) {
		return true;
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		return editor;
	}

	@Override
	protected Object getValue(Object element) {
		Torrent t = (Torrent)element;
		return t.getPriority().intValue();
	}

	@Override
	protected void setValue(Object element, Object value) {
		
		Torrent t = (Torrent)element;
		Long val = new Long((Integer)value);
		ChangeTorrentPriorityCommand cmd = new ChangeTorrentPriorityCommand();
		Map<String, String> params = new HashMap<String, String>();
		params.put(ChangeTorrentPriorityCommand.P_PRIORITY, val.toString());
		ExecutionEvent event = new ExecutionEvent(null, params, t, null);
		try {
			cmd.execute(event);
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}

}
